﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeCounsel.SharePoint.RibbonDesigner.Model;
using System.Windows;
using System.Windows.Input;
using CodeCounsel.SharePoint.RibbonDesigner.View.AttachedBehaviors;
using System.ComponentModel;

namespace CodeCounsel.SharePoint.RibbonDesigner.ViewModel
{
    public class ControlRefViewModel
        : BaseViewModel //, IDragProcessor, IDropProcessor
    {
        Func<string, IEnumerable<Control>> _controlSelector;
        ControlRef _controlRef;
        BaseViewModel _content;

        public BaseViewModel Content 
        {
            get { return _content; }
            private set
            {
                if (_content != value)
                {
                    _content = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Content"));
                }
            }
        }

        public DisplayMode DisplayMode
        {
            get { return _controlRef.DisplayMode; }
        }

        public ControlRefViewModel(ControlRef item, Func<string, IEnumerable<Control>> controlSelector)
        {
            _controlRef = item;
            _controlSelector = controlSelector;
            SetContent(item, controlSelector);
        }

        void SetContent(ControlRef item, Func<string, IEnumerable<Control>> controlSelector)
        {
            Control control = controlSelector(item.TemplateAlias).FirstOrDefault();
            if (control != null)
            {
                Content = ControlViewModelFactory.CreateControlModel(control, DisplayMode);
            }
            else
            {
                Content = new EmptyControlViewModel(DisplayMode);
            }
        }

    }
}
